<?php
include_once substr(__FILE__, 0, strrpos(__FILE__, '/')).'/SocialNetwork.php';

define('APIURL','https://api.twitter.com');
define('REQUESTTOKEN','/oauth/request_token');
define('AUTHORIZEPATH','/oauth/authorize?oauth_token=');
define('ACCESSTOKENPATH','/oauth/access_token');
 
class Twitter
    extends SocialNetwork
{
    public function  __construct($applicationName, $apiKey, $secretKey, $callback = null) {
        parent::__construct($applicationName, $apiKey, $secretKey, $callback);
        $this->networkName = 'Twitter';
        $this->apiUrl = APIURL;
        $this->requestTokenPath = REQUESTTOKEN;
        $this->authorizePath = AUTHORIZEPATH;
        $this->accessTokenPath = ACCESSTOKENPATH;
    }

    /**
     * Get link to requestToken
     * @param $username the username of the Twitter user. 
     * @return result of $this->fetch
     */
    public function requestToken($username)
    {
        $link = $this->requestTokenUrl;
        $link.= '&oauth_callback='.urlencode($this->callback);
    }

    /**
     * Save the status update to twitter.
     * @param $text string to be posted as the status
     * @return result of $this->fetch
     */
    public function statusUpdate($text) {

            $this->authorize();

            $parameters['status'] = $text;

            try {
                    $this->oauth->enableDebug();
                    //$this->oauth->fetch($this->apiUrl.'/v1/people/~/current-status',$parameters,OAUTH_HTTP_METHOD_PUT);
                    $this->oauth->fetch($this->apiUrl.'/1/statuses/update.xml',$parameters,OAUTH_HTTP_METHOD_POST);
                    return true;
            } catch(Exception $e) {
                    echo '<pre>';print_r($this->oauth->debugInfo);
                    echo '<pre>Error: POST current-status. Please try again.</pre>';
                    return false;
            }

    }

    /**
     * Get the information about a twitter user
     * @param $username name of the twitter user
     * @return array
     */
    public function getUserInfo($username) {

            $this->authorize();

            try {
                    $query = '/1/users/show.xml?screen_name='.$username;
                    return $this->get($this->apiUrl.$query);
            } catch(Exception $e) {
                    echo '<pre>Error: POST current-status. Please try again.</pre>';
                    return false;
            }

    }

    /**
     * Adds user to list
     * @param $username name of the twitter user
     * @param $listname name of the list
     * @return array
     */
    public function userToList($username, $listuser, $listname) {

            $this->authorize();

            $parameters['id'] = $username;
            $parameters['list_id'] = $listname;

            try {
                    $this->oauth->enableDebug();
                    //$this->oauth->fetch($this->apiUrl.'/v1/people/~/current-status',$parameters,OAUTH_HTTP_METHOD_PUT);
                    $this->oauth->fetch($this->apiUrl.'/1/'.$listuser.'/'.$listname.'/members.xml',$parameters,OAUTH_HTTP_METHOD_POST);
                    return true;
            } catch(Exception $e) {
                echo '<pre>';print_r($this->oauth->debugInfo);
                    echo '<pre>Error: POST current-status. Please try again.</pre>';
                    return false;
            }

    }

    /*
     * Gets the public timeline
	 * @return array
     */
    public function getPublicTimeLine(){
        $this->authorize();

        try {
            $query = '/1/statuses/public_timeline.xml';
            return $this->get($this->apiUrl.$query);
        } catch(Exception $e) {
            echo '<pre>Error: POST current-status. Please try again.</pre>';
            return false;
        }
    }

    /*
     * Gets the users home timeline
	 * @return array
     */
    public function getHomeTimeLine(){
        $this->authorize();

        try {
            $query = '/1/statuses/home_timeline.xml';
            return $this->get($this->apiUrl.$query);
        } catch(Exception $e) {
            echo '<pre>Error: POST current-status. Please try again.</pre>';
            return false;
        }
    }

}
